﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ControlerLib;
using System.IO;
using System.Diagnostics;

namespace EITSurferInfinityLib
{
    /// <summary>
    /// Interaction logic for SerialControl.xaml
    /// </summary>
    public partial class SerialControl : UserControl
    {
        VirtualDevice Device;
        UserControlSerialConnection currentsourcetester;
        UserControlSerialConnection robot;
        UserControlSerialConnection zmodule;

        public SerialControl(ref VirtualDevice device)
        {
            InitializeComponent();
            Device = device;

            currentsourcetester=new UserControlSerialConnection(ref Device,"currentsourcetester");
            robot=new UserControlSerialConnection(ref Device, "robot");
            zmodule = new UserControlSerialConnection(ref Device, "zmodule");

            grid_currentsource.Children.Add(currentsourcetester);
            grid_robot.Children.Add(robot);
            grid_zmodule.Children.Add(zmodule);

            ReadPortFromFile();
        }

        private void ReadPortFromFile()
        {
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(@"hardware settings\misc\serial.txt"))
                {
                    String line;
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] split = line.Split(' ');
                        string Target = split[0];
                        switch (Target)
                        {
                            case "zmodule":
                                currentsourcetester.SetPortName(split[1].Trim());
                                currentsourcetester.SetBaud(split[2].Trim());
                                break;
                            case "currentsourcetester":
                                zmodule.SetPortName(split[1].Trim());
                                zmodule.SetBaud(split[2].Trim());
                                break;
                            case "robot":
                                robot.SetPortName(split[1].Trim());
                                robot.SetBaud(split[2].Trim());
                                break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }
    }
}
